package com.cloudspace.yunloan.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;

import com.cloudspace.yunloan.R;
import com.cloudspace.yunloan.constants.Constants;
import com.cloudspace.yunloan.entity.CaseEntity;
import com.cloudspace.yunloan.fragment.CardValidFragment;
import com.cloudspace.yunloan.fragment.PersonSignFragment;
import com.cloudspace.yunloan.global.Res;
import com.cloudspace.yunloan.utils.AppPreferences;

public class CaseActivity extends YunloanActivity implements View.OnClickListener {
    private View firstStepNView;
    private View secondStepNView;

    private ImageView stepBg1Img;
    private ImageView stepBg2Img;

    private CardValidFragment cardValidFragment;
    private PersonSignFragment personSignFragment;

    public static  final  int INDEX_VALID = 0;
    public static  final  int INDEX_SIGN = 1;

    private CaseEntity mCaseEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_card_valid, true);
        getTitleBar().setSimpleMode(Res.getString(R.string.card_valid_title));

        firstStepNView = findViewById(R.id.left_item1);
        secondStepNView = findViewById(R.id.left_item2);
        firstStepNView.setOnClickListener(this);
        secondStepNView.setOnClickListener(this);

        stepBg1Img = findViewById(R.id.step_bg1);
        stepBg2Img = findViewById(R.id.step_bg2);

        if (getIntent() != null && getIntent().getExtras() != null){
            mCaseEntity = (CaseEntity) getIntent().getExtras().getSerializable("case_entity");
            AppPreferences.putString(Constants.CURRENT_CARD_ID, mCaseEntity.idCard);
        }else{
            mCaseEntity = new CaseEntity();
            mCaseEntity.state = 1;
        }

        if (mCaseEntity != null && mCaseEntity.state == 2){
            //目前不能点第一步
            firstStepNView.setEnabled(false);
            videoSignViewSelect();
        }else{
            authViewSelect();
        }
    }


    public static void showActivity(Context context, CaseEntity caseEntity){
        Intent intent = new Intent(context, CaseActivity.class);

        Bundle bundle = new Bundle();
        bundle.putSerializable("case_entity", caseEntity);
        intent.putExtras(bundle);

        if (!(context instanceof Activity)){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
         if (v.getId() == R.id.left_item1){
            authViewSelect();
        }else if (v.getId() == R.id.left_item2){
            videoSignViewSelect();
        }
    }

    private void authViewSelect() {
        firstStepNView.setBackground(getResources().getDrawable(R.drawable.cardvalid_left_bg_1));
        secondStepNView.setBackground(getResources().getDrawable(R.drawable.cardvalid_left_bg_2));

        stepBg1Img.setImageResource(R.drawable.yellow_step1);
        stepBg2Img.setImageResource(R.drawable.grey_step2);

        select(INDEX_VALID);
    }

    private void videoSignViewSelect() {
        firstStepNView.setBackground(getResources().getDrawable(R.drawable.cardvalid_left_bg_2));
        secondStepNView.setBackground(getResources().getDrawable(R.drawable.cardvalid_left_bg_1));

        stepBg1Img.setImageResource(R.drawable.grey_step1);
        stepBg2Img.setImageResource(R.drawable.yellow_step2);
        select(INDEX_SIGN);
    }

    public void select(int i) {
        //实例事物
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        switch (i) {
            case INDEX_VALID:
                if (cardValidFragment == null) {
                    cardValidFragment = new CardValidFragment();
                    cardValidFragment.setData(mCaseEntity);
                }
                transaction.replace(R.id.fl_main1_fragment, cardValidFragment, "cardValidFragment");
                break;

            case INDEX_SIGN:
                if (personSignFragment == null) {
                    personSignFragment = new PersonSignFragment();
                }
                transaction.replace(R.id.fl_main1_fragment, personSignFragment, "personSignFragment");
                break;

            default:
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (cardValidFragment != null){
            cardValidFragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
